package com.yao.sdk.net.volley;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/11/12.
 */
public class CookieRequest extends Request<Object> {


    private Map<String, String> header = new HashMap<String, String>();
    private Map<String, String> params = new HashMap<String, String>();
    CookieListener listener;
    ResultSet resultSet = new ResultSet();



    public class ResultSet {
        public String responseInfo;
        public Map<String, String> responseHeader;
    }

    public interface CookieListener {
        void onSuccess(ResultSet resultSet);
    }
    public CookieRequest(int method, String url, CookieListener listener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        if (listener == null) {
            throw new NullPointerException();
        }
        this.listener = listener;


    }

    public void setHeader( Map<String, String> header) {
        if (header != null) {
            this.header.putAll(header);
        }
    }

    public void setParam( Map<String, String> param) {
        this.params = param;
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return params;
    }

    @Override
    protected void deliverResponse(Object response) {
        listener.onSuccess(resultSet);
    }

    @Override
    protected Response parseNetworkResponse(NetworkResponse response) {
        resultSet.responseInfo = new String(response.data);
        resultSet.responseHeader = response.headers;
        return Response.success(resultSet, HttpHeaderParser.parseCacheHeaders(response));
    }



    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return header;
    }



}
